import { createHmac } from 'crypto'

import { APP_SECRETS } from '../constant'

/** 密码工具类 */
export class PasswordUtil {
  /** 比较 */
  static compare(newVal: string, oldVal: string) {
    const cryptoVal = this.crypto(newVal)
    return cryptoVal === oldVal
  }

  /** 加密 */
  static crypto(val: string) {
    const md5 = createHmac('sha256', APP_SECRETS.HASH_SECRET)
    // 强制转换成字符串，传数字会报错，
    return md5.update(String(val)).digest('hex')
  }
}
